package com.kartoshka.painters
{ 
	import com.kartoshka.interfaces.IPainter;

	import flash.display.Graphics;

	/**
	 * @author Alexander Zats, 2009
	 */
	public class BorderSolidPainter extends NullPainter implements IPainter 
	{

		public static const INNER : String = "inner";
		public static const OUTER : String = "outer";
		public static const CENTER : String = "center";

		protected var _color : uint;
		protected var _alpha : Number;
		protected var _thickness : Number;
		protected var _type : String;

		public function BorderSolidPainter( color : uint = 0x000000, alpha : Number = 1, thickness : Number = 1, cornerRadius : Number = 0, type : String = INNER )
		{
			this.color = color;
			this.alpha = alpha;
			this.thickness = thickness;
			this.cornerRadius = cornerRadius;
			this.type = type;
		}

		//TODO Нужна ли поддержка отрицательной ширины / высоты?
		override public function draw(graphics : Graphics, width : Number, height : Number) : void
		{
			var adjustedCornerRadius : Number;
			graphics.lineStyle( _thickness, _color, _alpha );
			switch ( _type )
			{
				case INNER:
					adjustedCornerRadius = _cornerRadius - _thickness;
					_cornerRadius ? graphics.drawRoundRect( _thickness / 2, _thickness / 2, width - _thickness, height - _thickness, adjustedCornerRadius, adjustedCornerRadius ) : graphics.drawRect( _thickness / 2, _thickness / 2, width - _thickness, height - _thickness );
					break;
				case OUTER:
					adjustedCornerRadius = _cornerRadius + _thickness;
					_cornerRadius ? graphics.drawRoundRect( - _thickness / 2, - _thickness / 2, width + _thickness, height + _thickness, adjustedCornerRadius, adjustedCornerRadius ) : graphics.drawRect( - _thickness / 2, - _thickness / 2, width + _thickness, height + _thickness );
					break;
				default:
					_cornerRadius ? graphics.drawRoundRect( 0, 0, width, height, _cornerRadius, _cornerRadius ) : graphics.drawRect( 0, 0, width, height );
					break;
			}
			//emulate graphics.endFill();
			graphics.lineStyle( 0, 0x000000, 0 );
		}

		public function set color( value : uint ) : void
		{
			if ( value > 0xFFFFFF ) color = 0xFFFFFF;
			_color = value;
		}

		public function get color() : uint
		{
			return _color;
		}

		public function set alpha( value : Number ) : void
		{
			if ( value < 0 ) value = 0;
			else if ( value > 1 ) value = 1;
			_alpha = value;
		}

		public function get alpha() : Number
		{
			return _alpha;
		}

		public function set thickness( value : Number ) : void
		{
			if ( value < 0 ) value = 0;
			_thickness = value;
		}

		public function get thickness() : Number
		{
			return _thickness;
		}

		public function set type( value : String ) : void
		{
			if ( value != INNER && value != OUTER ) value = CENTER;
			_type = value;
		}

		public function get type() : String
		{
			return _type;
		}
	}
}
